﻿package morn.core.components
{
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import morn.core.handlers.*;
    import morn.core.utils.*;

    public class Dialog extends View
    {
        protected var _dragArea:Rectangle;
        protected var _popupCenter:Boolean = true;
        protected var _closeHandler:Handler;
        public static const CLOSE:String = "close";
        public static const CANCEL:String = "cancel";
        public static const SURE:String = "sure";
        public static const NO:String = "no";
        public static const OK:String = "ok";
        public static const YES:String = "yes";

        public function Dialog()
        {
            return;
        }// end function

        override protected function initialize() : void
        {
            var _loc_1:* = getChildByName("drag");
            if (_loc_1)
            {
                this.dragArea = _loc_1.x + "," + _loc_1.y + "," + _loc_1.width + "," + _loc_1.height;
                removeElement(_loc_1);
            }
            addEventListener(MouseEvent.CLICK, this.onClick);
            return;
        }// end function

        protected function onClick(event:MouseEvent) : void
        {
            var _loc_2:* = event.target as Button;
            if (_loc_2)
            {
                switch(_loc_2.name)
                {
                    case CLOSE:
                    case CANCEL:
                    case SURE:
                    case NO:
                    case OK:
                    case YES:
                    {
                        this.close(_loc_2.name);
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            return;
        }// end function

        public function show(param1:Boolean = false) : void
        {
            App.dialog.show(this, param1);
            return;
        }// end function

        public function popup(param1:Boolean = false) : void
        {
            App.dialog.popup(this, param1);
            return;
        }// end function

        public function close(param1:String = null) : void
        {
            App.dialog.close(this);
            if (this._closeHandler != null)
            {
                this._closeHandler.executeWith([param1]);
            }
            return;
        }// end function

        public function get dragArea() : String
        {
            return StringUtils.rectToString(this._dragArea);
        }// end function

        public function set dragArea(param1:String) : void
        {
            var _loc_2:Array = null;
            if (Boolean(param1))
            {
                _loc_2 = StringUtils.fillArray([0, 0, 0, 0], param1);
                this._dragArea = new Rectangle(_loc_2[0], _loc_2[1], _loc_2[2], _loc_2[3]);
                addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDown);
            }
            else
            {
                this._dragArea = null;
                removeEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDown);
            }
            return;
        }// end function

        private function onMouseDown(event:MouseEvent) : void
        {
            if (this._dragArea.contains(mouseX, mouseY))
            {
                App.drag.doDrag(this);
            }
            return;
        }// end function

        public function get isPopup() : Boolean
        {
            return parent != null;
        }// end function

        public function get popupCenter() : Boolean
        {
            return this._popupCenter;
        }// end function

        public function set popupCenter(param1:Boolean) : void
        {
            this._popupCenter = param1;
            return;
        }// end function

        public function get closeHandler() : Handler
        {
            return this._closeHandler;
        }// end function

        public function set closeHandler(param1:Handler) : void
        {
            this._closeHandler = param1;
            return;
        }// end function

    }
}
